package com.szzg10.oracle.chapter39;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class BookshelfDML {

	public static void insertBookshelf(String title, String publisher, String categoryname, String rating) {
		
		String sql = "INSERT INTO BOOKSHELF VALUES (?, ?, ?, ?)";
		
		try {
			Connection connection = DriverManager.getConnection("jdbc:default:connection:");
			
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, title);
			preparedStatement.setString(2, publisher);
			preparedStatement.setString(3, categoryname);
			preparedStatement.setString(4, rating);
			
			preparedStatement.execute();
			preparedStatement.close();
		}
		catch(SQLException e) {
			System.err.println(e.getMessage());
		}
	}
	
	public static void updateBookshelf(String rating, String title) {
		
		String sql = "UPDATE BOOKSHELF SET RATING = ? " + "WHERE Title = ?";
		
		try {
			Connection connection = DriverManager.getConnection("jdbc:default:connection:");
			
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, rating);
			preparedStatement.setString(2, title);
			
			preparedStatement.executeUpdate();
			preparedStatement.close();
		}
		catch(SQLException e) {
			System.err.println(e.getMessage());
		}
	}
	
	public static void deleteBookshelf(String title) {
		
		String sql = "DELETE FROM BOOKSHELF WHERE TITLE = ?";
		
		try {
			Connection connection = DriverManager.getConnection("jdbc:default:connection:");
			
			PreparedStatement preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, title);
			
			preparedStatement.executeUpdate();
			preparedStatement.close();
		}
		catch(SQLException e) {
			System.err.println(e.getMessage());
		}
	}
}
